Completed
Push — master ( 0083cf...5b9113 )
by Elbert
41s
created

inject.js ➔ ... ➔ ???   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 8
nop 1
dl 0
loc 33
rs 6.6666
c 0
b 0
f 0
1
(() => {
2
	try {
3
    addEventListener('message', onMessage);
0 ignored issues
show
Bug introduced by
The local (let) variable onMessage is used before it is defined. This will cause a reference error.
Loading history...
4
5
    const onMessage = event => {
6
      if ( event.data.id !== 'patterns' ) {
7
        return;
8
      }
9
10
      removeEventListener('message', onMessage);
11
12
      const patterns = event.data.patterns || {};
13
14
      const js = {};
15
16
      for ( let appName in patterns ) {
17
        if ( patterns.hasOwnProperty(appName) ) {
18
          js[appName] = {};
19
20
          for ( let chain in patterns[appName] ) {
21
            if ( patterns[appName].hasOwnProperty(chain) ) {
22
              js[appName][chain] = {};
23
24
              for ( let index in patterns[appName][chain] ) {
25
                const value = detectJs(chain);
26
27
                if ( value && patterns[appName][chain].hasOwnProperty(index) ) {
28
                  js[appName][chain][index] = value;
29
                }
30
              }
31
            }
32
          }
33
        }
34
      }
35
36
      postMessage({ id: 'js', js }, '*');
37
    };
38
39
    const detectJs = chain => {
0 ignored issues
show
Unused Code introduced by
The constant detectJs seems to be never used. Consider removing it.
Loading history...
40
      const properties = chain.split('.');
41
42
      let value = properties.length ? window : null;
43
44
      for ( let i = 0; i < properties.length; i ++ ) {
45
        let property = properties[i];
46
47
        if ( value && value.hasOwnProperty(property) ) {
48
          value = value[property];
49
        } else {
50
          value = null;
51
52
          break;
53
        }
54
      }
55
56
      return typeof value === 'string' || typeof value === 'number' ? value : !!value;
57
    };
58
  } catch(e) {
59
    // Fail quietly
60
  }
61
})();
62